<?php

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Model\Room;
use App\Http\Services\ConfigServices;
use App\Http\Services\LoverRechargeServices;
use App\Http\Services\NewServices;
use App\Http\Services\NewYearServices;
use App\Http\Services\ShareDiamondServices;
use App\Http\Services\ShareSendGoodsServices;
use App\Http\Services\UserServices;
use App\Http\Services\WeekServices;
use Illuminate\Support\Facades\Config;
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Mockery\CountValidator\Exception;

class NewYearController extends BaseController
{
    protected $configServices;
    protected $userServices;
    protected $newYearServices;


    public function __construct()
    {
        //监听充值
        $this->configServices = new ConfigServices();
        $this->userServices = new UserServices();
        $this->newYearServices = new NewYearServices();
    }

    public function assignAction($body)
    {
        $body_date = time();
        if ($body['consumer_type'] == 1) {
            $body_date = $body['create'];
        }
        $config = $this->configServices->getConfig(14);
        $checkDate = $this->configServices->checkConfigDate($config['new_year_date'], $body_date);
        $cacheGift = $this->newYearServices->cacheGift($config['new_year_gift']['value']);
        if (!in_array($body['gift_id'], explode(',', $cacheGift))) {
            write_log('newYearConsumer', 'giftIdError', $body);//活动时间错误
            return false;
        }
        if (!$checkDate) {
            write_log('newYearConsumer', 'timeError', $body);//活动时间错误
            return false;
        }
        $this->listenGift($body);
        return true;
    }

    public function listenGift($body)
    {
        $gift_id = $body['gift_id'] ?? 0;
        $num = $body['gift_num'] ?? 0;
        $to_user_id_arr = $body['to_user_id_array'] ?? [];
        return $this->newYearServices->giftToNewUser($gift_id, $to_user_id_arr, $num);//礼物价格'
    }

    public function getUserGiftNum(Request $request)
    {
        $user_id = $request->input('user_id', 0);
        OutJson::outJsonResult(Hint::SUCCESS, $this->newYearServices->getUserGiftNum($user_id));
    }

    public function getUserRank(Request $request)
    {
        OutJson::outJsonResult(Hint::SUCCESS, $this->newYearServices->getRank());
    }

    public function clickGift(Request $request)
    {
        $user_id = $request->input('user_id', 0);
        $gift_id = $request->input('gift_id', 0);
        OutJson::outJsonResult(Hint::SUCCESS, $this->newYearServices->clickGift($user_id, $gift_id));
    }

    public function getHowe(Request $request)
    {
        OutJson::outJsonResult(Hint::SUCCESS, $this->newYearServices->getHowe());
    }

    //年度报告
    public function getYearlyReport(Request $request)
    {
        $user_id = $request->input('user_id');
        OutJson::outJsonResult(Hint::SUCCESS, $this->newYearServices->getYearlyReport($user_id));
    }

    public function getShareConfig(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $user_info = $this->userServices->getUserInfo($user_id);
        $config_data = $this->configServices->getConfig(15);
        $data['user_id'] = $user_id;
        $data['image'] = $user_info['portrait'];
        $data['title'] = $config_data['share_kk_wx_title']['value'] ?? '壳壳分享';
        $data['desc'] = $config_data['share_kk_wx_desc']['value'] ?? '欢迎来到壳壳语音';
        OutJson::outJsonNoData(Hint::SUCCESS, $data);
    }


    public function createPkRank()
    {
        $date = strtotime('2020-01-16');
        if (time() > $date) {
            return false;
        } else {
            OutJson::outJsonResult(Hint::SUCCESS, $this->newYearServices->createPkRank());
        }
    }

    public function getPkRank(Request $request)
    {
        $user_id = $request->input('user_id') ?? 0;
        $type = $request->input('type') ?? 0;
        OutJson::outJsonResult(Hint::SUCCESS, $this->newYearServices->getPkRank($user_id, $type));
    }

    public function awardMall()
    {
        $date = strtotime('2020-01-16');
        if (time() > $date) {
            return false;
        } else {
            OutJson::outJsonResult(Hint::SUCCESS, $this->newYearServices->awardMall());
        }
    }
}
